package com.zzty.txj1.util;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

public class GunZip {
    public static void unZipFiles(File zipFile,String descDir)throws IOException{
        long startTime=System.currentTimeMillis();
        File pathFile = new File(descDir);
        if(!pathFile.getParentFile().exists()){
            pathFile.getParentFile().mkdirs();
            if(pathFile.exists()){
                pathFile.mkdir();
            }
        }
        ZipFile zip =new ZipFile(zipFile,Charset.forName("GBK"));
        for(Enumeration entries = zip.entries();entries.hasMoreElements();){
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = zip.getInputStream(entry);
            String outPath = (descDir+"//"+zipEntryName).replaceAll("\\*", "/");
            //判断路径是否存在,不存在则创建文件路径
            File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
            if(!file.exists()){
                file.mkdirs();
            }
            //判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            if(new File(outPath).isDirectory()){
                continue;
            }
            //输出文件路径信息
            System.out.println(outPath);
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while((len=in.read(buf1))>0){
                out.write(buf1,0,len);
            }
            in.close();
            out.close();
        }
        System.out.println("******************解压完毕********************");
        long endTime=System.currentTimeMillis();
        System.out.println("耗费时间： "+(endTime-startTime)+" ms");
    }
    /**
     *
     * @Description (解压)
     * @param zipPath zip路径
     * @param charset 编码
     * @param outPutPath 输出路径
     */
    public static void decompression(String zipPath, String charset, String outPutPath) {
        long startTime=System.currentTimeMillis();
        try {
            ZipInputStream Zin=new ZipInputStream(new FileInputStream(zipPath), Charset.forName(charset));//输入源zip路径
            BufferedInputStream Bin=new BufferedInputStream(Zin);
            String Parent = outPutPath; //输出路径（文件夹目录）
            File Fout=null;
            ZipEntry entry;
            try {
                while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
                    Fout=new File(Parent,entry.getName());
                    if(!Fout.exists()){
                        (new File(Fout.getParent())).mkdirs();
                    }
                    FileOutputStream out=new FileOutputStream(Fout);
                    BufferedOutputStream Bout=new BufferedOutputStream(out);
                    int b;
                    while((b=Bin.read())!=-1){
                        Bout.write(b);
                    }
                    Bout.close();
                    out.close();
                    System.out.println(Fout+"解压成功");
                }
                Bin.close();
                Zin.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        long endTime=System.currentTimeMillis();
        System.out.println("耗费时间： "+(endTime-startTime)+" ms");
    }

    public static String  uncompress(String gzFileStr,String gunZipPath){
        try (ZipFile zf = new ZipFile(gzFileStr)){
            Enumeration<? extends ZipEntry> entries = zf.entries();
            while (entries.hasMoreElements()){
                ZipEntry entry = entries.nextElement();
                if(entry.isDirectory()){

                }
                String zipEntryName = entry.getName();
                File kkFile = new File(gunZipPath);
                if(!kkFile.getParentFile().exists()){
                    kkFile.getParentFile().mkdir();
                    if (!kkFile.exists()){
                        kkFile.mkdir();
                    }
                }

                String gunUrl=gunZipPath+"//"+zipEntryName;
                FileOutputStream fileOutputStream = new FileOutputStream(gunUrl);
                System.out.println(gunUrl);
                InputStream inputStream = zf.getInputStream(entry);
                byte[] b = new byte[1024*1024*5];
                int length = 0;
                while ((length = inputStream.read(b)) != -1) {
                    fileOutputStream.write(b, 0, length);
                }
                fileOutputStream.close();
                inputStream.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return gunZipPath;
    }
}
